<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>My Geek Time</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta
            name="viewport"
            content="width=device-width, initial-scale=1, maximum-scale=1"
    />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <link
            rel="stylesheet"
            title="default"
            href="https://unpkg.zhihu.com/amis@beta/sdk/sdk.css"
    />
    <link
            rel="stylesheet"
            title="default"
            href="https://unpkg.zhihu.com/amis@beta/sdk/sdk.css"
    />
    <link
            rel="stylesheet"
            href="https://unpkg.zhihu.com/amis@beta/sdk/helper.css"
    />
    <link
            rel="stylesheet"
            href="https://unpkg.zhihu.com/amis@beta/sdk/iconfont.css"
    />
    <script src="https://unpkg.zhihu.com/amis@beta/sdk/sdk.js"></script>
    <script src="https://unpkg.zhihu.com/vue@3"></script>
    <script src="https://unpkg.com/history@4.10.1/umd/history.js"></script>
    <style>
        html,
        body,
        .app-wrapper {
            position: relative;
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }
    </style>
</head>
<body>
<div id="root" class="app-wrapper"></div>
<script>
    (function () {

        // window.enableAMISDebug = true

        let amis = amisRequire('amis/embed');

        const match = amisRequire('path-to-regexp').match;

        // const history = History.createBrowserHistory();
        const history = History.createHashHistory();

        const login = {
            type: "page",
            body: [
                {
                    type: "wrapper",
                    body:  [
                        {
                            type: "service",
                            api: "/v2/base/config",
                            body: [
                                {
                                    type: "flex",
                                    mobile: {
                                        direction: "column"
                                    },
                                    alignItems: "center",
                                    style: {
                                        height: "100%",
                                        backgroundColor: "#fff"
                                    },
                                    items: [
                                        {
                                            style: {
                                                width: 500,
                                                height: 550,
                                                margin: 50,
                                            },
                                            type: "panel",
                                            title: "",
                                            body: [
                                                {
                                                    type: "flex",
                                                    mobile: {
                                                        direction: "column"
                                                    },
                                                    alignItems: "center",
                                                    style: {
                                                        height: "100%",
                                                    },
                                                    items: [
                                                        {
                                                            type: "html",
                                                            style: {
                                                                margin: 10
                                                            },
                                                            tpl: "<h2>您好，欢迎登录</h2><p>Hello, welcome to log in.</p>"
                                                        },
                                                    ]
                                                },
                                                {
                                                    type: "tabs",
                                                    tabsMode: "chrome",
                                                    id: "tabs-change-receiver",
                                                    tabs: [
                                                        {
                                                            visibleOn: "${ login_type=='email' }",
                                                            title: "邮箱登录",
                                                            body: {
                                                                type: "form",
                                                                title: "",
                                                                actions: [],
                                                                wrapWithPanel: false,
                                                                autoFocus: true,
                                                                api: {
                                                                    url: "/v2/base/login?waitSeconds=3",
                                                                    method: "post",
                                                                    data: {
                                                                        type: "${type}",
                                                                        data: {
                                                                            email: "${email}",
                                                                            password: "${password}",
                                                                        }
                                                                    },
                                                                    adaptor: function (payload, response, api, context) {
                                                                        if (payload.status === 0 && payload.token !== undefined) {
                                                                            localStorage.setItem("token", payload.token);
                                                                            localStorage.setItem("uname", payload.user.user_name);
                                                                            localStorage.setItem("uid", payload.user.uid);
                                                                            localStorage.setItem("role_id", payload.user.role_id);
                                                                            localStorage.setItem("access_token", payload.user.access_token);
                                                                            localStorage.setItem("avatar", payload.user.avatar);
                                                                            location.href="/";
                                                                        }
                                                                        return payload;
                                                                    }
                                                                },
                                                                body: [
                                                                    {
                                                                        label: "邮 箱｜Email",
                                                                        type: "input-email",
                                                                        required: true,
                                                                        validateOnChange: true,
                                                                        placeholder: "请输入邮箱",
                                                                        name: "email",
                                                                        value: "${ login_guest.name===undefined ? '' : login_guest.name }",
                                                                        validations: {
                                                                            isEmail: true,
                                                                        },
                                                                        validationErrors: {
                                                                            minLength: "邮箱不少于$1个字符"
                                                                        },
                                                                        borderMode: "half",
                                                                        style: {
                                                                            margin: 20
                                                                        },
                                                                    },
                                                                    {
                                                                        label: "密 码｜Password",
                                                                        type: "input-password",
                                                                        required: true,
                                                                        validateOnChange: true,
                                                                        placeholder: "请输入密码",
                                                                        name: "password",
                                                                        value: "${ login_guest.password===undefined ? '' : login_guest.password }",
                                                                        validations: {
                                                                            minLength: 6
                                                                        },
                                                                        validationErrors: {
                                                                            minLength: "密码不少于$1个字符"
                                                                        },
                                                                        borderMode: "half",
                                                                        style: {
                                                                            margin: 20
                                                                        },
                                                                    },
                                                                    {
                                                                        type: "hidden",
                                                                        name: "type",
                                                                        value: "email"
                                                                    },
                                                                    {
                                                                        type: "flex",
                                                                        alignItems: "center",
                                                                        mobile: {
                                                                            direction: "column"
                                                                        },
                                                                        style: {
                                                                            height: "100%",
                                                                            backgroundColor: "#fff"
                                                                        },
                                                                        items: [
                                                                            {
                                                                                label: "登 录｜Login",
                                                                                level: "light",
                                                                                size: "lg",
                                                                                type: "submit",
                                                                                style: {
                                                                                    margin: 20,
                                                                                },
                                                                            }
                                                                        ]
                                                                    },
                                                                ]
                                                            }
                                                        },
                                                        {
                                                            title: "用户名登录",
                                                            visibleOn: "${ login_type=='name' }",
                                                            body: {
                                                                type: "form",
                                                                title: "",
                                                                actions: [],
                                                                wrapWithPanel: false,
                                                                autoFocus: true,
                                                                api: {
                                                                    url: "/v2/base/login?waitSeconds=3",
                                                                    method: "post",
                                                                    data: {
                                                                        type: "${type}",
                                                                        data: {
                                                                            account: "${account}",
                                                                            password: "${password}",
                                                                        }
                                                                    },
                                                                    adaptor: function (payload, response, api, context) {
                                                                        if (payload.status === 0 && payload.token !== undefined) {
                                                                            localStorage.setItem("token", payload.token);
                                                                            localStorage.setItem("uname", payload.user.user_name);
                                                                            localStorage.setItem("uid", payload.user.uid);
                                                                            localStorage.setItem("role_id", payload.user.role_id);
                                                                            localStorage.setItem("avatar", payload.user.avatar);
                                                                            location.href="/";
                                                                        }
                                                                        return payload;
                                                                    }
                                                                },
                                                                body: [
                                                                    {
                                                                        label: "用户名｜Name",
                                                                        type: "input-text",
                                                                        required: true,
                                                                        validateOnChange: true,
                                                                        placeholder: "请输入用户名",
                                                                        name: "account",
                                                                        value: "${ login_guest.name===undefined ? '' : login_guest.name }",
                                                                        validations: {
                                                                            minLength: 6
                                                                        },
                                                                        validationErrors: {
                                                                            minLength: "用户名不少于$1个字符"
                                                                        },
                                                                        borderMode: "half",
                                                                        style: {
                                                                            margin: 20
                                                                        },
                                                                    },
                                                                    {
                                                                        label: "密 码｜Password",
                                                                        type: "input-password",
                                                                        required: true,
                                                                        validateOnChange: true,
                                                                        placeholder: "请输入密码",
                                                                        name: "password",
                                                                        value: "${ login_guest.password===undefined ? '' : login_guest.password }",
                                                                        validations: {
                                                                            minLength: 6
                                                                        },
                                                                        validationErrors: {
                                                                            minLength: "密码不少于$1个字符"
                                                                        },
                                                                        borderMode: "half",
                                                                        style: {
                                                                            margin: 20
                                                                        },
                                                                    },
                                                                    {
                                                                        type: "hidden",
                                                                        name: "type",
                                                                        value: "name"
                                                                    },
                                                                    {
                                                                        type: "flex",
                                                                        alignItems: "center",
                                                                        mobile: {
                                                                            direction: "column"
                                                                        },
                                                                        style: {
                                                                            height: "100%",
                                                                            backgroundColor: "#fff"
                                                                        },
                                                                        items: [
                                                                            {
                                                                                label: "登 录｜Login",
                                                                                level: "light",
                                                                                size: "lg",
                                                                                type: "submit",
                                                                                style: {
                                                                                    margin: 20,
                                                                                },
                                                                            }
                                                                        ]
                                                                    },
                                                                ]
                                                            }
                                                        },
                                                        {
                                                            title: "邮箱注册",
                                                            visibleOn: "${ register_type=='email' }",
                                                            body: {
                                                                type: "form",
                                                                title: "",
                                                                actions: [],
                                                                wrapWithPanel: false,
                                                                autoFocus: true,
                                                                api: {
                                                                    url: "/v2/base/register?waitSeconds=3",
                                                                    method: "post",
                                                                    data: {
                                                                        type: "${type}",
                                                                        data: {
                                                                            email: "${email}",
                                                                            code: "${code}",
                                                                            password: "${password}"
                                                                        }
                                                                    }
                                                                },
                                                                onEvent: {
                                                                    submitSucc: {
                                                                        actions: [
                                                                            {
                                                                                actionType: "changeActiveKey",
                                                                                componentId: "tabs-change-receiver",
                                                                                args: {
                                                                                    activeKey: 1
                                                                                }
                                                                            }
                                                                        ]
                                                                    }
                                                                },
                                                                body: [
                                                                    {
                                                                        label: "邮 箱｜Email",
                                                                        type: "input-email",
                                                                        required: true,
                                                                        placeholder: "请输入邮箱",
                                                                        name: "email",
                                                                        validateOnChange: true,
                                                                        validations: {
                                                                            isEmail: true,
                                                                            minLength: 5
                                                                        },
                                                                        validationErrors: {
                                                                            minLength: "邮箱不少于$1个字符"
                                                                        },
                                                                        borderMode: "half",
                                                                        strictMode: false,
                                                                        style: {
                                                                            margin: 5
                                                                        },
                                                                        addOn: {
                                                                            name: "countdown",
                                                                            label: "发送验证码",
                                                                            type: "button",
                                                                            level: "light",
                                                                            countDown: 120,
                                                                            strictMode: false,
                                                                            countDownTpl: "${timeLeft} 秒后重发",
                                                                            validations: {
                                                                                isEmail: true,
                                                                                minLength: 5
                                                                            },
                                                                            validationErrors: {
                                                                                minLength: "邮箱不少于$1个字符"
                                                                            },
                                                                            actionType: "ajax",
                                                                            api: {
                                                                                url:"/v2/base/send/email",
                                                                                method: "POST",
                                                                                data:{
                                                                                    email: "${email}",
                                                                                }
                                                                            }
                                                                        }
                                                                    },
                                                                    {
                                                                        name: "code",
                                                                        type: "input-text",
                                                                        required: true,
                                                                        label: "验证码｜Email Code",
                                                                        placeholder: "请输入邮箱验证码",
                                                                        borderMode: "half",
                                                                        strictMode: false,
                                                                        validateOnChange: true,
                                                                        validations: {
                                                                            minLength: 6
                                                                        },
                                                                        validationErrors: {
                                                                            minLength: "邮箱验证码不能为空"
                                                                        },
                                                                        style: {
                                                                            margin: 5
                                                                        },
                                                                    },
                                                                    {
                                                                        label: "密 码｜Password",
                                                                        type: "input-password",
                                                                        required: true,
                                                                        validateOnChange: true,
                                                                        placeholder: "请输入密码",
                                                                        name: "password",
                                                                        validations: {
                                                                            minLength: 6
                                                                        },
                                                                        validationErrors: {
                                                                            minLength: "密码不少于$1个字符"
                                                                        },
                                                                        borderMode: "half",
                                                                        style: {
                                                                            margin: 5
                                                                        },
                                                                    },
                                                                    {
                                                                        label: "确 认｜Confirm",
                                                                        type: "input-password",
                                                                        required: true,
                                                                        validateOnChange: true,
                                                                        strictMode: false,
                                                                        placeholder: "请输入确认密码",
                                                                        name: "confirm_password",
                                                                        validations: {
                                                                            minLength: 6,
                                                                            equalsField: "password"
                                                                        },
                                                                        validationErrors: {
                                                                            minLength: "密码不少于$1个字符",
                                                                            equalsField: "两次密码不一致"
                                                                        },
                                                                        borderMode: "half",
                                                                        style: {
                                                                            margin: 5
                                                                        },
                                                                    },
                                                                    {
                                                                        type: "hidden",
                                                                        name: "type",
                                                                        value: "email"
                                                                    },
                                                                    {
                                                                        type: "flex",
                                                                        alignItems: "center",
                                                                        mobile: {
                                                                            direction: "column"
                                                                        },
                                                                        style: {
                                                                            height: "100%",
                                                                            backgroundColor: "#fff"
                                                                        },
                                                                        items: [
                                                                            {
                                                                                label: "注 册｜Register",
                                                                                level: "light",
                                                                                size: "lg",
                                                                                type: "submit",
                                                                                style: {
                                                                                    margin: 10,
                                                                                },
                                                                            }
                                                                        ]
                                                                    },
                                                                ]
                                                            }
                                                        },
                                                        {
                                                            title: "用户名注册",
                                                            visibleOn: "${ register_type=='name' }",
                                                            body: {
                                                                type: "form",
                                                                title: "",
                                                                actions: [],
                                                                wrapWithPanel: false,
                                                                autoFocus: true,
                                                                api: {
                                                                    url: "/v2/base/register?waitSeconds=3",
                                                                    method: "post",
                                                                    data: {
                                                                        type: "${type}",
                                                                        data: {
                                                                            account: "${account}",
                                                                            password: "${password}",
                                                                        }
                                                                    }
                                                                },
                                                                onEvent: {
                                                                    submitSucc: {
                                                                        actions: [
                                                                            {
                                                                                actionType: "changeActiveKey",
                                                                                componentId: "tabs-change-receiver",
                                                                                args: {
                                                                                    activeKey: 1
                                                                                }
                                                                            }
                                                                        ]
                                                                    }
                                                                },
                                                                body: [
                                                                    {
                                                                        label: "用户名｜Name",
                                                                        type: "input-text",
                                                                        required: true,
                                                                        placeholder: "请输入用户名",
                                                                        name: "account",
                                                                        validateOnChange: true,
                                                                        validations: {
                                                                            minLength: 5
                                                                        },
                                                                        validationErrors: {
                                                                            minLength: "用户名不少于$1个字符"
                                                                        },
                                                                        borderMode: "half",
                                                                        style: {
                                                                            margin: 5
                                                                        }
                                                                    },
                                                                    {
                                                                        label: "密 码｜Password",
                                                                        type: "input-password",
                                                                        required: true,
                                                                        validateOnChange: true,
                                                                        placeholder: "请输入密码",
                                                                        name: "password",
                                                                        validations: {
                                                                            minLength: 6
                                                                        },
                                                                        validationErrors: {
                                                                            minLength: "密码不少于$1个字符"
                                                                        },
                                                                        borderMode: "half",
                                                                        style: {
                                                                            margin: 5
                                                                        },
                                                                    },
                                                                    {
                                                                        label: "确 认｜Confirm",
                                                                        type: "input-password",
                                                                        required: true,
                                                                        validateOnChange: true,
                                                                        strictMode: false,
                                                                        placeholder: "请输入确认密码",
                                                                        name: "confirm_password",
                                                                        validations: {
                                                                            minLength: 6,
                                                                            equalsField: "password"
                                                                        },
                                                                        validationErrors: {
                                                                            minLength: "密码不少于$1个字符",
                                                                            equalsField: "两次密码不一致"
                                                                        },
                                                                        borderMode: "half",
                                                                        style: {
                                                                            margin: 5
                                                                        },
                                                                    },
                                                                    {
                                                                        type: "hidden",
                                                                        name: "type",
                                                                        value: "name"
                                                                    },
                                                                    {
                                                                        type: "flex",
                                                                        alignItems: "center",
                                                                        mobile: {
                                                                            direction: "column"
                                                                        },
                                                                        style: {
                                                                            height: "100%",
                                                                            backgroundColor: "#fff"
                                                                        },
                                                                        items: [
                                                                            {
                                                                                label: "注 册｜Register",
                                                                                level: "light",
                                                                                size: "lg",
                                                                                type: "submit",
                                                                                style: {
                                                                                    margin: 10,
                                                                                },
                                                                            }
                                                                        ]
                                                                    },
                                                                ]
                                                            }
                                                        }
                                                    ],
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        };

        const app = {
            type: 'app',
            brandName: 'MyGeekTime',
            logo: '/public/logo.png',
            header: {
                type: "grid",
                columns: [
                    {
                        md: 9,
                    },
                    {
                        md: 0,
                        body: {
                            type: "flex",
                            mobile: {
                                direction: "column"
                            },
                            justify: "flex-end",
                            items: [
                                {
                                    type: "avatar",
                                    src: "${ls:avatar}",
                                    text: "${ls:uname}",
                                    onError: "return true;",
                                },
                                {
                                    type: "dropdown-button",
                                    level: "link",
                                    label: "${ls:uname}",
                                    buttons: [
                                        {
                                            type: "button",
                                            level: "secondary",
                                            label: "刷新凭据",
                                            size: "sm",
                                            actionType: "drawer",
                                            visibleOn: "${ ls:role_id === 1 }",
                                            drawer: {
                                                resizable: true,
                                                closeOnOutside: true,
                                                actions: [],
                                                size: "md",
                                                title: "cookie登录",
                                                body: [
                                                    "【极客时间VIP登录凭据】，该登录凭据是全站共享的基础,下载期间避免失效，失效后下载会找不到下载链接  ",
                                                    "<a href='https://zkep.github.io/mygeektime/guide/data_geektime/' target='_blank'>查看详细文档</a>",
                                                    {
                                                        type: "form",
                                                        title: "",
                                                        actions: [],
                                                        wrapWithPanel: false,
                                                        api: {
                                                            url: "/v2/base/refresh/cookie?waitSeconds=3",
                                                            method: "post",
                                                            data: {
                                                                cookie: "${cookie}"
                                                            }
                                                        },
                                                        body: [
                                                            {
                                                                label: "Cookie",
                                                                type: "textarea",
                                                                showCounter: true,
                                                                clearable: true,
                                                                required: true,
                                                                placeholder: "请输入极客时间Cookie",
                                                                name: "cookie",
                                                                validations: {
                                                                    minLength: 50
                                                                },
                                                                validationErrors: {
                                                                    minLength: "Cookie不少于$1个字符"
                                                                },
                                                                borderMode: "half",
                                                                style: {
                                                                    margin: 20
                                                                },
                                                            },
                                                            {
                                                                type: "flex",
                                                                alignItems: "center",
                                                                style: {
                                                                    height: "100%",
                                                                    backgroundColor: "#fff"
                                                                },
                                                                items: [
                                                                    {
                                                                        label: "刷 新｜Refresh",
                                                                        level: "light",
                                                                        size: "lg",
                                                                        type: "submit",
                                                                        style: {
                                                                            margin: 20,
                                                                        },
                                                                    }
                                                                ]
                                                            },
                                                        ]
                                                    }
                                                ]
                                            }
                                        },
                                        {
                                            type: "button",
                                            level: "secondary",
                                            label: "退出",
                                            size: "sm",
                                            onClick: "localStorage.clear(); document.cookie = 'analogjwt=; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';location.href='/';"
                                        }
                                    ]
                                }
                            ]
                        }
                    }
                ]
            },
            footer: '<div class="p-2 text-center bg-light">My Geek Time</div>',
            api: '/pages/site.json'
        };


        function getCookie(name) {
            return (
                decodeURIComponent(
                    document.cookie.replace(
                        new RegExp(
                            "(?:(?:^|.*;)\\s*" +
                            encodeURIComponent(name).replace(/[-.+*]/g, "\\$&") +
                            "\\s*\\=\\s*([^;]*).*$)|^.*$",
                        ),
                        "$1",
                    ),
                ) || null
            );
        }

        function normalizeLink(to, location = history.location) {
            to = to || '';

            if (to && to[0] === '#') {
                to = location.pathname + location.search + to;
            } else if (to && to[0] === '?') {
                to = location.pathname + to;
            }

            const idx = to.indexOf('?');
            const idx2 = to.indexOf('#');
            let pathname = ~idx
                ? to.substring(0, idx)
                : ~idx2
                    ? to.substring(0, idx2)
                    : to;
            let search = ~idx ? to.substring(idx, ~idx2 ? idx2 : undefined) : '';
            let hash = ~idx2 ? to.substring(idx2) : location.hash;

            if (!pathname) {
                pathname = location.pathname;
            } else if (pathname[0] != '/' && !/^https?\:\/\//.test(pathname)) {
                let relativeBase = location.pathname;
                const paths = relativeBase.split('/');
                paths.pop();
                let m;
                while ((m = /^\.\.?\//.exec(pathname))) {
                    if (m[0] === '../') {
                        paths.pop();
                    }
                    pathname = pathname.substring(m[0].length);
                }
                pathname = paths.concat(pathname).join('/');
            }

            return pathname + search + hash;
        }

        function isCurrentUrl(to, ctx) {
            if (!to) {
                return false;
            }
            const pathname = history.location.pathname;
            const link = normalizeLink(to, {
                ...location,
                pathname,
                hash: ''
            });

            if (!~link.indexOf('http') && ~link.indexOf(':')) {
                let strict = ctx && ctx.strict;
                return match(link, {
                    decode: decodeURIComponent,
                    strict: typeof strict !== 'undefined' ? strict : true
                })(pathname);
            }

            return decodeURI(pathname) === link;
        }

        const token =  localStorage.getItem("token") ||  getCookie("analogjwt");

        let amisInstance = amis.embed(
            '#root',
            token === null ? login : app,
            {
                location: history.location,
                data: {
                    token: token
                },
                context: {
                    // API_HOST: ''
                }
            },
            {
                // watchRouteChange: fn => {
                //   return history.listen(fn);
                // },
                requestAdaptor: function (api) {
                    // if(api.url.indexOf("/v1/api/") > -1) {
                    //   api.url = "http://localhost:9226" + api.url;
                    // }
                    return {
                        ...api,
                        headers: {
                            ...api.headers,
                            Authorization: "Bearer "+token
                        }
                    };
                },
                responseAdaptor(api, payload, query, request, response) {
                    // console.log('responseAdaptor',payload)
                    if(payload.status === 401 || payload.status === 400 || payload.status === 403) {
                        localStorage.clear();
                        document.cookie = 'analogjwt=; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
                        location.href="/";
                    }
                    return {
                        ...payload,
                    };
                },
                updateLocation: (location, replace) => {
                    location = normalizeLink(location);
                    if (location === 'goBack') {
                        return history.goBack();
                    } else if (
                        (!/^https?\:\/\//.test(location) &&
                            location ===
                            history.location.pathname + history.location.search) ||
                        location === history.location.href
                    ) {
                        // 目标地址和当前地址一样，不处理，免得重复刷新
                        return;
                    } else if (/^https?\:\/\//.test(location) || !history) {
                        return (window.location.href = location);
                    }

                    history[replace ? 'replace' : 'push'](location);
                },
                jumpTo: (to, action) => {
                    if (to === 'goBack') {
                        return history.goBack();
                    }

                    to = normalizeLink(to);

                    if (isCurrentUrl(to)) {
                        return;
                    }

                    if (action && action.actionType === 'url') {
                        action.blank === false
                            ? (window.location.href = to)
                            : window.open(to, '_blank');
                        return;
                    } else if (action && action.blank) {
                        window.open(to, '_blank');
                        return;
                    }

                    if (/^https?:\/\//.test(to)) {
                        window.location.href = to;
                    } else if (
                        (!/^https?\:\/\//.test(to) &&
                            to === history.pathname + history.location.search) ||
                        to === history.location.href
                    ) {
                        // do nothing
                    } else {
                        history.push(to);
                    }
                },
                isCurrentUrl: isCurrentUrl,
                theme: 'cxd'
            }
        );

        history.listen(state => {
            amisInstance.updateProps({
                location: state.location || state
            });
        });

    })();
</script>
</body>
</html>